package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.EverydayRecommendActivity;
import flc.ast.adapter.CalendarAdapter;
import flc.ast.adapter.EverydayRecommendAdapter;
import flc.ast.bean.CalendarBean;
import flc.ast.databinding.FragmentCalendarBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.view.container.StkLinearLayout;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CalendarFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CalendarFragment extends BaseNoModelFragment<FragmentCalendarBinding> {

    private CalendarAdapter calendarAdapter;
    private List<CalendarBean> listCalendar = new ArrayList<>();
    private int oldPosition;
    private EverydayRecommendAdapter everydayRecommendAdapter;
    private List<String> listUrl = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.fragment_calendar;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.tvRecommendMore.setOnClickListener(this);
        mDataBinding.rvCalendarList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        calendarAdapter = new CalendarAdapter();
        mDataBinding.rvCalendarList.setAdapter(calendarAdapter);
        calendarAdapter.setOnItemClickListener(this);
        mDataBinding.rvRecommendList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        everydayRecommendAdapter = new EverydayRecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(everydayRecommendAdapter);
        everydayRecommendAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvRecommendMore:
                EverydayRecommendActivity.url=listUrl.get(oldPosition);
                startActivity(EverydayRecommendActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter==calendarAdapter){
            calendarAdapter.getItem(oldPosition).setSelected(false);
            oldPosition = position;
            calendarAdapter.getItem(oldPosition).setSelected(true);
            calendarAdapter.notifyDataSetChanged();
            mDataBinding.tvDate.setText(calendarAdapter.getItem(position).getDate());
            getData(listUrl.get(position));
        }else {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            saveRecord(bean);
            BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
        }
    }

    @Override
    protected void initData() {
        //每日推荐
        listUrl.add(AppConfig.WEEK_DATA1);
        listUrl.add(AppConfig.WEEK_DATA2);
        listUrl.add(AppConfig.WEEK_DATA3);
        listUrl.add(AppConfig.WEEK_DATA4);
        listUrl.add(AppConfig.WEEK_DATA5);
        listUrl.add(AppConfig.WEEK_DATA6);
        listUrl.add(AppConfig.WEEK_DATA7);
        //日历
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        setData(calendar, Calendar.MONDAY, format, getString(R.string.week_text1));
        setData(calendar, Calendar.TUESDAY, format, getString(R.string.week_text2));
        setData(calendar, Calendar.WEDNESDAY, format, getString(R.string.week_text3));
        setData(calendar, Calendar.THURSDAY, format, getString(R.string.week_text4));
        setData(calendar, Calendar.FRIDAY, format, getString(R.string.week_text5));
        setData(calendar, Calendar.SATURDAY, format, getString(R.string.week_text6));
        setData(calendar, Calendar.SUNDAY, format, getString(R.string.week_text7));
        calendarAdapter.setList(listCalendar);
    }

    //设置日期并判断是否为今天
    private void setData(Calendar calendar, int mode, SimpleDateFormat format, String week) {
        calendar.set(Calendar.DAY_OF_WEEK, mode);
        listCalendar.add(new CalendarBean(TimeUtils.date2String(calendar.getTime(), format), week));
        if (TimeUtils.isToday(calendar.getTime())) {
            oldPosition = listCalendar.size() - 1;
            listCalendar.get(oldPosition).setSelected(true);
            mDataBinding.tvDate.setText(TimeUtils.date2String(calendar.getTime(), format));
            getData(listUrl.get(oldPosition));
        }
    }

    private void getData(String url) {
        StkResApi.getTagResourceList(this, url, StkResApi.createParamMap(0, 6), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        everydayRecommendAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void saveRecord(StkResBean bean) {
        List<StkResBean> listSave = new ArrayList<>();
        listSave.add(bean);
        List<StkResBean> listSp = AppSpUtils.getReadRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveReadRecordList(listSp);
        } else {
            AppSpUtils.saveReadRecordList(listSave);
        }
    }

}